package com.itcast.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.itcast.bean.product.ProductType;
import com.itcast.service.product.ProductTypeService;
import com.opensymphony.xwork2.ActionSupport;

@Controller("product")
public class ProductAction extends ActionSupport {
	
	ProductTypeService productTypeService;
	
	private String name;
	private String note;
	private Integer parentId;
	private Integer pagaNumber = 0;
	private Integer pagaSize = 3;
	
	ProductType productType;
	List<ProductType> list;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public Integer getPagaNumber() {
		return pagaNumber;
	}

	public void setPagaNumber(Integer pagaNumber) {
		this.pagaNumber = pagaNumber;
	}

	public Integer getPagaSize() {
		return pagaSize;
	}

	public void setPagaSize(Integer pagaSize) {
		this.pagaSize = pagaSize;
	}

	public ProductType getProductType() {
		return productType;
	}

	public void setProductType(ProductType productType) {
		this.productType = productType;
	}

	public List<ProductType> getList() {
		return list;
	}

	public void setList(List<ProductType> list) {
		this.list = list;
	}

	public String add() {
		productType = new ProductType(name, note);
		if (parentId != null) {
			productType.setParentType(new ProductType(parentId));
		}
		System.out.print(parentId);
		productTypeService.save(productType);
		//productTypeService.delete(6);
		return SUCCESS; 
	}
	
	public String delete() {
//        HttpServletRequest req = ServletActionContext.getRequest();
//        req.setAttribute("s", s);
//        System.out.print(req.getAttribute("s"));
		return SUCCESS; 
	}
	
	public String getScrollData() {
		HttpServletRequest request = ServletActionContext.getRequest();   
		int offset = 0;
		if(request.getParameter("pager.offset")!=null)  
			offset=Integer.parseInt(request.getParameter("pager.offset"));
		if (parentId == null) {
			list = productTypeService.findList(offset , 3);
		} else {
			list = productTypeService.findListForWhere(parentId, 0, 10);
		}
		return SUCCESS; 
	}

	public ProductTypeService getProductTypeService() {
		return productTypeService;
	}

	@Resource(name="productTypeServiceBean")
	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}
}
